package com.jialea.springboot.config;

import com.jialea.springboot.config.properties.Knife4jProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * @author: wf
 * @created: 2024/6/22 16:25
 */
@Configuration
public class Knife4jConfig {

    @Autowired
    private Knife4jProperties knife4jProperties;

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(knife4jProperties.getEnable())
                .apiInfo(adminApiInfo())
                .select()
                //只显示路径下的页面
                .apis(RequestHandlerSelectors.basePackage(knife4jProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 文档信息配置
     */
    private ApiInfo adminApiInfo() {
        return new ApiInfoBuilder()
                .title(knife4jProperties.getTitle())
                .description(knife4jProperties.getDescription())
                .version(knife4jProperties.getVersion())
                .contact(new Contact(knife4jProperties.getAuthor(), knife4jProperties.getAddress(), knife4jProperties.getEmail()))
                .build();
    }
}

